package chapter15.Exercise;

import java.util.ArrayList;
import java.util.Comparator;

/**
 * @Auther: Tx
 * @Date: 2022/5/26 21:34
 */
public class Ex02 {
    public static void main(String[] args) {
        ArrayList<Employee> list = new ArrayList<>();
        list.add(new Employee("tom", 2000, new Mydate(2000, 10, 1)));
        list.add(new Employee("jack", 20000, new Mydate(2001, 06, 1)));
        list.add(new Employee("smith", 200000, new Mydate(2002, 06, 1)));
        list.add(new Employee("smith", 200000, new Mydate(2003, 06, 1)));
        System.out.println(list);
//        实现定制排序
        list.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                if (!(o1 instanceof Employee && o2 instanceof Employee)) {
                    System.out.println("类型不正确");
                    return 0;

                }
                int i = o1.getName().compareTo(o2.getName());
                if (i!=0){
                    return i;
                }
          return o1.getBirthday().compareTo(o2.getBirthday());

            }
        });
        System.out.println("=====排序后的结果====");
        for (Employee e : list) {
            System.out.println(e);
        }
    }
}

class Employee  {
    private String name;
    private double sal;
    private Mydate birthday;

    public Employee(String name, double sal, Mydate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public Mydate getBirthday() {
        return birthday;
    }

    public void setBirthday(Mydate birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday +
                '}';
    }


}

class Mydate implements Comparable<Mydate>{
    private int year;
    private int month;
    private int day;

    public Mydate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    @Override
    public int compareTo(Mydate o) {
        int minusYear = year - o.getYear();
        if (minusYear!=0){
            return minusYear;
        }
        int minusMonth = month - o.getMonth();

        if (minusYear!=0){
            return minusMonth;
        }
        return day=o.getDay();
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public String toString() {
        return "Mydate{" +
                "year='" + year + '\'' +
                ", month='" + month + '\'' +
                ", day='" + day + '\'' +
                '}';
    }
}